// 导出一个矩形类

/**
 * 矩形基类
 */
export class Rectangle {
    /**
     * 矩形类
     * @param {*} width   宽
     * @param {*} height  高
     * @param {*} left    横坐标
     * @param {*} top     纵坐标
     * @param {*} xspeed  x 速度
     * @param {*} yspeed  y 速度
     * @param {*} dom     元素
     */
    constructor(width, height, left, top, xspeed, yspeed, dom) {
        this.width = width;
        this.height = height;
        this.left = left;
        this.top = top;
        this.xspeed = xspeed;
        this.yspeed = yspeed;
        this.dom = dom;

        // 初始化渲染
        this.render();
    }

    // 渲染函数
    render() {
        this.dom.style.width = this.width + "px";
        this.dom.style.height = this.height + "px";
        this.dom.style.top = this.top + "px";
        this.dom.style.left = this.left + "px";
    }
    /**
     * 移动函数
     * @param {*} duration  时间
     */
    move(duration) {
        const x = this.xspeed * duration;
        const y = this.yspeed * duration;
        // 改变 left, top 坐标
        this.left = this.left + x;
        this.top = this.top + y;
        // 判断子类是否有该方法
        if (this.onMove) {
            this.onMove();
        }
        // 重新渲染
        this.render();
    }
}